<?php
	/**
	* Simpleswich v1.00a.
	* ---------------------------------------------------------------------------
	* indix.php (main loading page)
	*/ 

    # show all errors for dev
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

	# include the conf file (make sure this is on top) 
	# this is all the paths and usernames and passwords
	include_once("configs/config.inc.php");
	
	# create a connection to the database (TODO add error handeling)
	include_once("inc/Database.class.php");
	$dbh = new Database(array(
		'server' => $cfg['database']['address'],
		'user'   => $cfg['database']['username'],
		'password' => $cfg['database']['password'],
		'database' => 'Simpleswitch'
	));			

	# class to manage users
	include_once("inc/Users.class.php");
	$users = new Users($dbh);

	# RPC class will me our main interaction with freeswitch
	include("inc/FreeswitchRPC.class.php");
	$client = new FreeswitchRPC(
						$cfg['rpc']['address'], 
						$cfg['rpc']['port'], 
						$cfg['rpc']['username'], 
						$cfg['rpc']['password']
					);


    $client = new xmlrpc_client("/RPC2", "localhost", 8081);
    $client->setCredentials('freeswitch','works',NULL);



	# set to smarty and smarty paths
    require('lib/Smarty/Smarty.class.php');
    $smarty = new Smarty();
    $smarty->template_dir = 'templates';
    $smarty->compile_dir = 'templates_c';
    $smarty->cache_dir = 'cache';
    $smarty->config_dir = 'configs';
    
    # part of plugin crap that should change soon
    $is_plugin = 0;  
    
    # default page
    $page = 'login.tpl'; 

	# xss protection it may help..
	include("inc/xss.class.php");
    
    // Start the session and set all session vars to smarty vars 
    session_start();
    if(isset($_SESSION['user_info'])){
        $smarty->assign('user_info', $_SESSION['user_info']);
    }
    
    # assign some other handy smarty vars
    $smarty->assign('remote_addr', $_SERVER['REMOTE_ADDR']);
    
    
    //this is the main control
    $action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';

    # read in all plugins (TODO: redo plugin interface)
    $plugins = Array();
    foreach (glob("plugins/*.php") as $filename) {
    	include_once($filename);
    }	    
    $smarty->assign('plugins', $plugins);

	# main control of the flow of the page
    switch ($action) {
        case "login":
            $page = 'login.tpl';
			break;
        
        case "do_login":
            $username = $_POST['username'];
            $password = $_POST['password'];
			if($info = $users->auth_user($username, $password)){
                //show main user page
                $_SESSION['user_info'] = $info;
                # reset the sessin info in smarty so we can use it on the main page 
                $smarty->assign('user_info', $_SESSION['user_info']);
                $page = 'main.tpl';
            }
            else{
                $smarty->assign('error', "bad username or password.");
                $page = 'login.tpl';
            }
            break;
        
        case "users": # add and manage users
            $page = 'users.tpl';
            break;
        
        case "plugins": 
        	# extend Simpleswitch with user plugins 
        	# myabe we can connect this to a comunity deiven plugin interface
            $page = 'plugins.tpl';
            break;
        
        case "do_logout":
        	# remove the session and kick user back to login page
            session_destroy();
            $_SESSION['user_info'] = NULL;
            $smarty->assign('user_info', $_SESSION['user_info']);
            $page = 'login.tpl';
            break;
        
        default: # no action set so go to main or login page
			if(isset($_SESSION['user_info'])){
				if(!$is_plugin){
					$page = 'main.tpl';
				}
            }
            else{ # not logged in
                $page = 'login.tpl';
            }
    }
    $smarty->display($page);
?>
